package com.mrdeer.iot.processor.impl;

import com.mrdeer.iot.bean.ChannelInfo;
import com.mrdeer.iot.bean.command.Protocol;
import com.mrdeer.iot.bean.enums.EMQExchange;
import com.mrdeer.iot.bean.enums.ERTUChannelFlag;
import com.mrdeer.iot.constant.FrameConst;
import com.mrdeer.iot.processor.IDataProcessor;
import com.mrdeer.iot.processor.ProcessorAbstract;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

/**
 * 系统信息 处理器
 *
 * @Author: xie jianchu
 */
@Slf4j
public class ControlProcessor extends ProcessorAbstract implements IDataProcessor {

    private static final String SIGNAL = "Signal";
    private static final String VERSION = "version";
    private static final String PING = "ping";
    private static final String SN = "sn";

    public ControlProcessor() {
        super(ERTUChannelFlag.CONTROLE);
    }

    @Override
    public void translate(ChannelHandlerContext ctx, ChannelInfo channelInfo, Protocol protocol) throws Exception {
        //物联网id,指标信息,是否发布,消息队列标示
        channelInfo.setProtocol(protocol);
        if (protocol.getType().equals(FrameConst.control)) {
            //控制指令
            EMQExchange[] emqExchanges = {EMQExchange.RTU_CONTROL_EXCHANGE};
            channelInfo.setEmqExchanges(emqExchanges);
        } else if (protocol.getType().equals(FrameConst.status)) {
            //状态
            EMQExchange[] emqExchanges = {EMQExchange.RTU_STATUS_EXCHANGE};
            channelInfo.setEmqExchanges(emqExchanges);
        } else if (protocol.getType().equals(FrameConst.invalid)) {
            //无效指令
            EMQExchange[] emqExchanges = {EMQExchange.RTU_INVALID_EXCHANGE};
            channelInfo.setEmqExchanges(emqExchanges);
        } else if (protocol.getType().equals(FrameConst.heartbeat)) {
            //心跳
            EMQExchange[] emqExchanges = {EMQExchange.RTU_HEART_EXCHANGE};
            channelInfo.setEmqExchanges(emqExchanges);
        } else {
            if (super.getNextProcessor() != null) {
                super.getNextProcessor().translate(ctx, channelInfo, protocol);
            }
        }
    }
}
